package com.zzty.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DBUtil {
    static PropertyUtil props=new PropertyUtil();
    private static final String driver = props.getProp().getProperty("driver");
    private static final String url=props.getProp().getProperty("url");
    private static final String userName=props.getProp().getProperty("userName");
    private static final String password=props.getProp().getProperty("password");
    private static Connection cn = null;
    private static PreparedStatement pst = null;
    // 加载驱动
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 创建连接
    public static Connection getConnection() throws Exception {
        if (cn == null) {
            cn = DriverManager.getConnection(url, userName, password);
        }
        return cn;
    }
    // 获取数据
    public static PreparedStatement getPreparedStatement(String sql) throws Exception {
        cn = getConnection();
        pst = cn.prepareStatement(sql);
        return pst;
    }
    // 关闭连接
    public static void close() throws Exception {
        cn.close();
        pst.close();

    }
}
